﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ResourceTypeConfigurationApp
{
    public partial class fmCompanyData : Form
    {
        public fmCompanyData()
        {
            InitializeComponent();
        }

        private async void btnSearch_Click(object sender, EventArgs e)
        {

            //开始结束时间不可为空
            if (dtpStart.Value == null || dtpEnd.Value == null)
            {
                MessageBox.Show("开始结束时间不可为空");
                return;
            }
            //开始时间不可大于结束时间
            if (dtpStart.Value > dtpEnd.Value)
            {
                MessageBox.Show("开始时间不可大于结束时间");
                return;
            }

            var dtA = await MySQLHelper.GetDataTableBySqlAsync(MySQLHelper.targetConnectionString, $@"
SELECT
	cro.id,
	cro.factory_station_id,
	cro.fname,
	cro.custom_no
FROM
	custom_resource_object cro
	LEFT JOIN custom_resource_type crt ON cro.custom_resource_type_id = crt.id 
WHERE 
	cro.custom_resource_type_id = 1 AND (cro.fname LIKE '%{txtSiteName.Text}%' OR  cro.custom_no LIKE '%{txtSiteName.Text}%')ORDER BY cro.fname");

            if (!dgSite.Columns.Contains("IsSelected"))
            {
                // 创建一个DataGridViewCheckBoxColumn
                DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
                checkBoxColumn.HeaderText = "选中";
                checkBoxColumn.Name = "IsSelected";
                checkBoxColumn.DataPropertyName = "IsSelected";
                // 将勾选列添加到DataGridView
                dgSite.Columns.Add(checkBoxColumn);
            }

            // 为DataTable添加对应的列
            dtA.Columns.Add("IsSelected", typeof(bool));
            dtA.Columns["IsSelected"].SetOrdinal(0); // 将勾选列设置为第一列

            // 设置不允许新增行
            dgSite.AllowUserToAddRows = false;
            dgSite.AllowUserToDeleteRows = false; // 禁止删除行

            SiteBindingSource.DataSource = dtA;
            dgSite.DataSource = SiteBindingSource;

            //dataGridView1.DataSource = dtA;
            dgSite.Columns["id"].HeaderText = "ID";
            dgSite.Columns["factory_station_id"].HeaderText = "站点ID";
            dgSite.Columns["fname"].HeaderText = "企业名称";
            dgSite.Columns["custom_no"].HeaderText = "户号";
            dgSite.AutoResizeColumns();

            dgSite.CellContentClick += dgSite_CellContentClick;
            lbSCount.Text = dtA.Rows.Count.ToString();

            // 设置不允许新增行
            dgDevice.AllowUserToAddRows = false;

            dgDevice.AllowUserToDeleteRows = false; // 禁止删除行

            if (DeviceBindingSource.DataSource != null)
            {
                (DeviceBindingSource.DataSource as DataTable).Rows.Clear();
            }


        }

        private async void dgSite_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // 检查点击的是否是IsSelected列
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == dgSite.Columns["IsSelected"].Index)
                {
                    for (int i = 0; i < dgSite.Rows.Count; i++)
                    {
                        if (i != e.RowIndex)
                        {
                            // 取消其他行IsSelected列的勾选
                            dgSite.Rows[i].Cells["IsSelected"].Value = false;
                        }
                    }
                    //var dr = SiteBindingSource.Current as DataRowView;
                    DataRowView drv = SiteBindingSource[e.RowIndex] as DataRowView;
                    if (drv == null)
                    {
                        return;
                    }
                    {

                        string strSql = $@"
SELECT
	site_gen_pt_15min_json.day,
	site_gen_pt_15min_json.`value` 
FROM
	site_gen_pt_15min_json 
WHERE
	site_gen_pt_15min_json.type = 1 
	AND site_gen_pt_15min_json.pkey = '{drv["ID"]}' 
    AND site_gen_pt_15min_json.day BETWEEN '{dtpStart.Value}' AND '{dtpEnd.Value}'
ORDER BY site_gen_pt_15min_json.DAY";

                        if (rbWh.Checked)
                        {
                            strSql = $@"
SELECT
	site_gen_wh_15min_json.DAY,
	site_gen_wh_15min_json.sup_wh_value as `value`
FROM
	site_gen_wh_15min_json 
WHERE
	site_gen_wh_15min_json.type = 1 
	AND site_gen_wh_15min_json.pkey = '{drv["ID"]}' 
    AND site_gen_wh_15min_json.day BETWEEN '{dtpStart.Value}' AND '{dtpEnd.Value}'
ORDER BY site_gen_wh_15min_json.DAY";
                        }

                        var dtA = await MySQLHelper.GetDataTableBySqlAsync(MySQLHelper.targetConnectionString, strSql);

                        DeviceBindingSource.DataSource = dtA;
                        dgDevice.DataSource = DeviceBindingSource;

                        dgDevice.Columns["day"].HeaderText = "日期";
                        dgDevice.Columns["value"].HeaderText = "96点数据";
                        dgDevice.AutoResizeColumns();

                        lbCount.Text = dtA.Rows.Count.ToString();
                    }
                    
                }
            }

        }


        private void tsmAllSelect_Click(object sender, EventArgs e)
        {
            DataTable dtA = SiteBindingSource.DataSource as DataTable;
            if (dtA != null && dtA.Rows.Count > 0)
            {
                //for (int i = 0; i < dgDevice.Rows.Count; i++)
                //{
                //    // 取消其他行IsSelected列的勾选
                //    dgDevice.Rows[i].Cells["IsSelected"].Value = true;
                //}

                foreach (var dr in dtA.AsEnumerable())
                {
                    if (dr.RowState == DataRowState.Deleted)
                    {
                        continue;
                    }

                    dr["IsSelected"] = true;
                    dr.EndEdit();
                }
                dgDevice.EndEdit();

            }
        }

        private void tsmAllCancel_Click(object sender, EventArgs e)
        {
            DataTable dtA = SiteBindingSource.DataSource as DataTable;
            if (dtA != null && dtA.Rows.Count > 0)
            {
                //for (int i = 0; i < dgDevice.Rows.Count; i++)
                //{
                //    // 取消其他行IsSelected列的勾选
                //    dgDevice.Rows[i].Cells["IsSelected"].Value = true;
                //}

                foreach (var dr in dtA.AsEnumerable())
                {
                    if (dr.RowState == DataRowState.Deleted)
                    {
                        continue;
                    }

                    dr["IsSelected"] = false;
                    dr.EndEdit();
                }
                dgDevice.EndEdit();

            }
        }



        private void fmCompanyData_Load(object sender, EventArgs e)
        {

            //初始时间为当天
            dtpStart.Value = DateTime.Now.Date;
            dtpEnd.Value = DateTime.Now.Date;
        }
    }
}
